package com.dup.jxlh.admin.controller;

import com.dup.jxlh.admin.entities.model.Department;
import com.dup.jxlh.admin.service.DepartmentService;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/department")
@Validated
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/getDepartmentList")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_DepartmentSet_Show')")
    public CommonResult getDepartmentList(PaginationData paginationData) {
        return ResultUtil.result(ResultEnum.SUCCESS,
                departmentService.getDepartmentList(paginationData));
    }

    @PostMapping("/addDepartment")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_DepartmentSet_Operate')")
    public CommonResult addDepartment(@Validated Department department) {
        if (departmentService.addDepartment(department)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/editDepartment")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_DepartmentSet_Operate')")
    public CommonResult editDepartment(@Validated Department department) {
        department.setCompanyId(null);
        if (departmentService.editDepartment(department)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

}
